<?php

namespace App\Common\Utils;

class RsaUtils {

    /**
     * 获取私钥     
     * @return bool|resource     
     */
    private static function getPrivateKey() {
        $absPath = public_path() . '/rsa_private_key.pem';
        $content = file_get_contents($absPath);
        return openssl_pkey_get_private($content);
    }

    /**
     * 获取公钥     
     * @return bool|resource     
     */
    private static function getPublicKey() {
        $absPath = public_path() . '/key/rsa_public_key.pem';
        $content = file_get_contents($absPath);
        return openssl_pkey_get_public($content);
    }

    /**
     * 私钥加密     
     * @param string $data     
     * @return null|string     
     */
    public static function privEncrypt($data = '') {
        if (!is_string($data)) {
            return null;
        }
        $eccryptDatas = mbStrSplit($data, 117);
        $html = '';
        foreach ($eccryptDatas as $key => $val) {
            $html .= openssl_private_encrypt($val, $encrypted, self::getPrivateKey()) ? base64_encode($encrypted) : null;
        }
        return $html;
        //return openssl_private_encrypt($data,$encrypted,self::getPrivateKey()) ? base64_encode($encrypted) : null;    
    }

    /**
     * 公钥加密     
     * @param string $data     
     * @return null|string     
     */
    public static function publicEncrypt($data = '') {
        if (!is_string($data)) {
            return null;
        }
        $publicEccryptDatas = mbStrSplit($data, 117);
        $html = '';
        foreach ($publicEccryptDatas as $key => $val) {
            $html .= openssl_public_encrypt($val, $encrypted, self::getPublicKey()) ? base64_encode($encrypted) : null;
        }
        return $html;
        //return openssl_public_encrypt($data,$encrypted,self::getPublicKey()) ? base64_encode($encrypted) : null;    
    }

    /**
     * 私钥解密     
     * @param string $encrypted     
     * @return null     
     */
    public static function privDecrypt($encrypted = '') {
        if (!is_string($encrypted)) {
            return null;
        }
        $privDecryptDatas = explode('=', $encrypted);
        $html = '';
        foreach ($privDecryptDatas as $key => $val) {
            $val = str_replace(' ', '+', $val);
            $html .= openssl_private_decrypt(base64_decode($val), $decrypted, self::getPrivateKey()) ? $decrypted : null;
        }
        return $html;
        //return (openssl_private_decrypt(base64_decode($encrypted), $decrypted, self::getPrivateKey())) ? $decrypted : null;    
    }

    /**
     * 公钥解密     
     * @param string $encrypted     
     * @return null     
     */
    public static function publicDecrypt($encrypted = '') {
        if (!is_string($encrypted)) {
            return null;
        }
        $ecryptedDatas = explode('=', $encrypted);
        $html = '';
        foreach ($ecryptedDatas as $key => $val) {
            $html .= openssl_public_decrypt(base64_decode($val), $decrypted, self::getPublicKey()) ? $decrypted : null;
        }
        return $html;
        //return (openssl_public_decrypt(base64_decode($encrypted), $decrypted, self::getPublicKey())) ? $decrypted : null;    
    }

}
